﻿using System;
using System.Configuration;
using System.Data;
using System.Xml;
using Soveryn.Commerce.Shared;

/// <summary>
/// This class handles all most website content data transfer and caching between the Soveryn Core and the website.
/// </summary>

namespace Soveryn.Commerce.API
{

    public class WebsiteData
    {
        public static String WebsiteAPIKey = ConfigurationManager.AppSettings["WebsiteAPIKey"];
        public static String WebsiteAPIURL = ConfigurationManager.AppSettings["WebsiteAPIURL"];

        /// <summary>
        /// Return details of a blog post
        /// </summary>
        public static DataSet BlogPostDetails(int BlogPostID)
        {

            DataSet ds = null;
            ds = CacheUtils.GetCachedDataSet("Blog", "BlogPost_" + BlogPostID);
            if (ds == null)
            {

                ds = Soveryn.Commerce.Shared.API.CallForDataSet(WebsiteAPIURL, "BlogPostDetails", new
                {
                    WebsiteAPIKey = WebsiteAPIKey,
                    BlogPostID = BlogPostID
                }, false, true);

                CacheUtils.SetCachedDataSet("Blog", "BlogPost_" + BlogPostID, ds);
                ds = CacheUtils.GetCachedDataSet("Blog", "BlogPost_" + BlogPostID);

            }

            return ds;

        }

        /// <summary>
        /// Return a list of most recent blog posts in a specific category
        /// </summary>
        public static DataSet BlogPostListTopByCategory(int Count, int BlogCategoryID)
        {

            DataSet ds = null;
            ds = CacheUtils.GetCachedDataSet("Blog", "BlogPostListTopByCategory_" + Count + "_" + BlogCategoryID);
            if (ds == null)
            {

                ds = Soveryn.Commerce.Shared.API.CallForDataSet(WebsiteAPIURL, "BlogPostListTopByCategory", new
                {
                    WebsiteAPIKey = WebsiteAPIKey,
                    Count = Count,
                    BlogCategoryID = BlogCategoryID
                }, false, true);

                CacheUtils.SetCachedDataSet("Blog", "BlogPostListTopByCategory_" + Count + "_" + BlogCategoryID, ds);
                ds = CacheUtils.GetCachedDataSet("Blog", "BlogPostListTopByCategory_" + Count + "_" + BlogCategoryID);

            }

            return ds;

        }

        /// <summary>
        /// Return a list of blog posts in a specific category
        /// </summary>
        public static DataSet BlogPostListByCategory(int BlogCategoryID)
        {

            DataSet ds = null;
            ds = CacheUtils.GetCachedDataSet("Blog", "BlogPostList_" + BlogCategoryID);
            if (ds == null)
            {

                ds = Soveryn.Commerce.Shared.API.CallForDataSet(WebsiteAPIURL, "BlogPostListByCategory", new
                {
                    WebsiteAPIKey = WebsiteAPIKey,
                    BlogCategoryID = BlogCategoryID
                }, false, true);

                CacheUtils.SetCachedDataSet("Blog", "BlogPostList_" + BlogCategoryID, ds);
                ds = CacheUtils.GetCachedDataSet("Blog", "BlogPostList_" + BlogCategoryID);

            }

            return ds;

        }

        /// <summary>
        /// Return a list the latest blog posts
        /// </summary>
        public static DataSet BlogPostListTop(int Count)
        {

            DataSet ds = null;
            ds = CacheUtils.GetCachedDataSet("Blog", "BlogPostListTop_" + Count);
            if (ds == null)
            {

                ds = Soveryn.Commerce.Shared.API.CallForDataSet(WebsiteAPIURL, "BlogPostListTop", new
                {
                    WebsiteAPIKey = WebsiteAPIKey,
                    Count = Count
                }, false, true);

                CacheUtils.SetCachedDataSet("Blog", "BlogPostListTop_" + Count, ds);
                ds = CacheUtils.GetCachedDataSet("Blog", "BlogPostListTop_" + Count);

            }

            return ds;

        }

        /// <summary>
        /// Return the details of a blog category
        /// </summary>
        public static DataSet BlogCategoryDetails(int BlogCategoryID)
        {

            DataSet ds = null;
            ds = CacheUtils.GetCachedDataSet("Blog", "BlogCategoryDetails_" + BlogCategoryID);
            if (ds == null)
            {

                ds = Soveryn.Commerce.Shared.API.CallForDataSet(WebsiteAPIURL, "BlogCategoryDetails", new
                {
                    WebsiteAPIKey = WebsiteAPIKey,
                    BlogCategoryID = BlogCategoryID
                }, false, true);

                CacheUtils.SetCachedDataSet("Blog", "BlogCategoryDetails_" + BlogCategoryID, ds);
                ds = CacheUtils.GetCachedDataSet("Blog", "BlogCategoryDetails_" + BlogCategoryID);

            }

            return ds;

        }

        /// <summary>
        /// Return the details of the website
        /// </summary>
        public static WebsiteProperties WebsiteDetail()
        {

            DataSet ds = null;
            ds = CacheUtils.GetCachedDataSet("Website", "WebsiteDetail");
            if (ds == null)
            {

                ds = Soveryn.Commerce.Shared.API.CallForDataSet(WebsiteAPIURL, "WebsiteDetailByAPIKey", new { 
                    WebsiteAPIKey = WebsiteAPIKey
                }, false, true);
     
                CacheUtils.SetCachedDataSet("Website", "WebsiteDetail", ds);
                ds = CacheUtils.GetCachedDataSet("Website", "WebsiteDetail");

            }

            return WebsiteProperties.Create(ds);

        }

        /// <summary>
        /// Return the details of a category
        /// </summary>
        public static CategoryProperties CategoryDetail(String CategoryID)
        {

            DataSet ds = null;
            ds = CacheUtils.GetCachedDataSet("Category", "CategoryDetail_" + CategoryID);
            if (ds == null)
            {

                ds = Soveryn.Commerce.Shared.API.CallForDataSet(WebsiteAPIURL, "CategoryDetail", new
                {
                    WebsiteAPIKey = WebsiteAPIKey,
                    CategoryID = CategoryID
                }, false, true);

                CacheUtils.SetCachedDataSet("Category", "CategoryDetail_" + CategoryID, ds);
                ds = CacheUtils.GetCachedDataSet("Category", "CategoryDetail_" + CategoryID);
            }

            return CategoryProperties.Create(ds);

        }

        /// <summary>
        /// Return the entire page tree structure in XML form
        /// </summary>
        public static XmlDocument PageList()
        {

            DataSet ds = null;

            ds = CacheUtils.GetCachedDataSet("Website", "PageList");
            if (ds == null)
            {

                ds = Soveryn.Commerce.Shared.API.CallForDataSet(WebsiteAPIURL, "PageList", new
                {
                    WebsiteAPIKey = WebsiteAPIKey
                }, false, true);

                CacheUtils.SetCachedDataSet("Website", "PageList", ds);
                ds = CacheUtils.GetCachedDataSet("Website", "PageList");
            }

            //do data relation
            DataRelation relation = new DataRelation("ParentChild", ds.Tables[0].Columns["PageID"], ds.Tables[0].Columns["ParentPageID"], true);
            relation.Nested = true;
            ds.Relations.Add(relation);

            //transform it into xml
            XmlDocument PageListXML = new XmlDocument();
            PageListXML.LoadXml(ds.GetXml());

            return PageListXML;

        }

        /// <summary>
        /// Return the details of a page
        /// </summary>
        public static DataSet PageDetails(int PageID)
        {
            DataSet ds = null;

            ds = CacheUtils.GetCachedDataSet("Page", "PageDetails_" + PageID);
            if (ds == null)
            {

                ds = Soveryn.Commerce.Shared.API.CallForDataSet(WebsiteAPIURL, "PageDetails", new
                {
                    WebsiteAPIKey = WebsiteAPIKey,
                    PageID = PageID
                }, false, true);


                CacheUtils.SetCachedDataSet("Page", "PageDetails_" + PageID, ds);
                ds = CacheUtils.GetCachedDataSet("Page", "PageDetails_" + PageID);
            }

            return ds;

        }

        /// <summary>
        /// Return the details of an item
        /// </summary>
        public static DataSet ItemDetail(int ItemID)
        {
            DataSet ds = null;

            ds = CacheUtils.GetCachedDataSet("Item", "ItemDetail_" + ItemID);

            if (ds == null)
            {

                ds = Soveryn.Commerce.Shared.API.CallForDataSet(WebsiteAPIURL, "ItemDetail", new
                {
                    WebsiteAPIKey = WebsiteAPIKey,
                    ItemID = ItemID
                }, false, true);

                CacheUtils.SetCachedDataSet("Item", "ItemDetail_" + ItemID, ds);
                ds = CacheUtils.GetCachedDataSet("Item", "ItemDetail_" + ItemID);
            }

            return ds;

        }

        /// <summary>
        /// Return a list of items based on several parameters (doesn't cache results)
        /// </summary>
        public static DataSet ItemSearch_nocache(String SearchTerm, String CatagoryIDsCommaDelim, Boolean UseORCatagoryInsteadOfAnd, Boolean ReturnSKUData, Boolean EfficientMode)
        {

            DataSet ds  = Soveryn.Commerce.Shared.API.CallForDataSet(WebsiteAPIURL, "ItemSearch", new
            {
                WebsiteAPIKey = WebsiteAPIKey,
                SearchTerm = SearchTerm,
                CatagoryIDsCommaDelim = CatagoryIDsCommaDelim,
                UseORCatagoryInsteadOfAnd = UseORCatagoryInsteadOfAnd,
                ReturnSKUData = ReturnSKUData,
                EfficientMode = EfficientMode
            }, false, true);

            return ds;

        }

        /// <summary>
        /// Return a list of template pages for a page
        /// </summary>
        public static DataSet PageTemplateListForPage(int PageID)
        {

            DataSet ds = Soveryn.Commerce.Shared.API.CallForDataSet(WebsiteAPIURL, "PageTemplateListForPage", new
            {
                WebsiteAPIKey = WebsiteAPIKey,
                PageID = PageID
            }, false, true);

            return ds;

        }

        /// <summary>
        /// Return a list of items that belong to a certain page and a list of categories
        /// </summary>
        public static DataSet ItemListForPageID(int PageID, String CatagoryIDsCommaDelim)
        {

            DataSet ds = null;

            ds = CacheUtils.GetCachedDataSet("ItemList", "ItemListForPageID_" + PageID);
            if (ds == null)
            {
                //we use a string of category IDs to pull list but we use PAGEID to identify in the cache hence the need for both pageIDs and category IDs.    
                ds = ItemSearch_nocache(null, CatagoryIDsCommaDelim, false, true, false);
                CacheUtils.SetCachedDataSet("ItemList", "ItemListForPageID_" + PageID, ds);
                ds = CacheUtils.GetCachedDataSet("ItemList", "ItemListForPageID_" + PageID);

            }

            return ds;

        }

        /// <summary>
        /// Return a list of items that belong to a specific category
        /// </summary>
        public static DataSet ItemListForCategoryID(String CatagoryID)
        {

            DataSet ds = null;

            ds = CacheUtils.GetCachedDataSet("ItemList", "ItemListForCategoryID_" + CatagoryID);
            if (ds == null)
            {
                ds = ItemSearch_nocache(null, CatagoryID, false, true, false);
                CacheUtils.SetCachedDataSet("ItemList", "ItemListForCategoryID_" + CatagoryID, ds);
                ds = CacheUtils.GetCachedDataSet("ItemList", "ItemListForCategoryID_" + CatagoryID);

            }

            return ds;

        }

        /// <summary>
        /// Return a list of items that have the GlobalFeatured order field set
        /// </summary>
        public static DataSet ItemListOfGlobalFeatured()
        {
            DataSet ds = null;

            ds = CacheUtils.GetCachedDataSet("Website", "ItemListOfGlobalFeatured");
            if (ds == null)
            {

                ds = Soveryn.Commerce.Shared.API.CallForDataSet(WebsiteAPIURL, "ItemListOfGlobalFeatured", new
                {
                    WebsiteAPIKey = WebsiteAPIKey
                }, false, true);

                CacheUtils.SetCachedDataSet("Website", "ItemListOfGlobalFeatured", ds);
                ds = CacheUtils.GetCachedDataSet("Website", "ItemListOfGlobalFeatured");
            }

            return ds;

        }

        /// <summary>
        /// Return a list of countries
        /// </summary>
        public static DataSet LocationCountryList()
        {

            DataSet ds = Soveryn.Commerce.Shared.API.CallForDataSet(WebsiteAPIURL, "LocationCountryList", new
            {
                WebsiteAPIKey = WebsiteAPIKey
            }, false, true);

            return ds;
        }

        /// <summary>
        /// Return a list of states/provinces from a country code
        /// </summary>
        public static DataSet LocationStateProvList(String CountryIDPostalAbrev)
        {

            DataSet ds = Soveryn.Commerce.Shared.API.CallForDataSet(WebsiteAPIURL, "LocationStateProvList", new
            {
                WebsiteAPIKey = WebsiteAPIKey,
                CountryIDPostalAbrev = CountryIDPostalAbrev
            }, false, true);

            return ds;
        }


    }

}